<?php
if (2==2) {
	# code...
}
if(!defined ('TYPO3_MODE'))		die('Access denied.');

$feUserObj = tslib_eidtools::initFeUser();
$user_id = $feUserObj->user['ses_userid'];

function loggedIn($displayError = false) {
	global $feUserObj;
	
	if(is_array($feUserObj->user)) {
		return true;
	} else {
		if($displayError) {
			echo "Error: User not logged in";
		}
		return false;
	}
}
tslib_eidtools::connectDB();

switch(t3lib_div::_GET('function')) {
	// Functions only available when logged in
	case 'submitQuiz':
		if(!loggedIn(true)) break;
		
		$atom_id = intval(t3lib_div::_GET('atom'));
		$option = t3lib_div::_GET('option');

		if($atom_id) {
			$res = $TYPO3_DB->exec_SELECTgetRows('data','tx_enotelms_atom_userdata','atom_id='.intval($atom_id) . ' AND crfeuser_id='.intval($user_id));
			$option_clean = array();
			
			// Make options clean for MySQL
			foreach($option as $theIdx => $quizzes) {
				foreach($quizzes as $id => $questions) {
					foreach($questions as $num => $answers) {
						foreach($answers as $count => $answer) {
							if(!is_array($answer)){
								$option_clean[intval($theIdx)][intval($id)][intval($num)][intval($count)] = mysql_real_escape_string($answer);
							}
						}
					}
				}
			}
			if ($res[0]['data']) {
				$field_values['crfeuser_id'] = intval($user_id);
				$field_values['atom_id'] = intval($atom_id);
				$existingData = unserialize($res[0]['data']);
				$mergedData = $existingData;
				foreach ($option_clean as $pageIdx => $quizzes) { 
					if(array_key_exists($pageIdx, $existingData)) {
						foreach($quizzes as $id => $quiz) {
							if(array_key_exists($id,$existingData[$pageIdx])) {
								foreach($quiz as $count => $question) {
									if(array_key_exists($count,$existingData[$pageIdx][$id])) {
										// Do nothing, question HAS been answered once
									} else {
										$mergedData[$pageIdx][$id][$count] = $option_clean[$pageIdx][$id][$count];
									}
								}
							} else {
								$mergedData[$pageIdx][$id] = $option_clean[$pageIdx][$id];
							}
						}
					} else {
						$mergedData[$pageIdx] =  $option_clean[$pageIdx];
					}
				}
				$field_values['data'] = serialize($mergedData);	
		
				$TYPO3_DB->exec_UPDATEquery('tx_enotelms_atom_userdata','atom_id='.intval($atom_id).' AND crfeuser_id=' .intval($user_id),$field_values);
			} else {
				$field_values['crfeuser_id'] = intval($user_id);
				$field_values['atom_id'] = intval($atom_id);
				$field_values['data'] = serialize($option_clean);
		
				$TYPO3_DB->exec_INSERTquery('tx_enotelms_atom_userdata',$field_values);
			}
			//print_r(unserialize($field_values['data']));
		} else {
			echo "Not allowed";
		}
	break;
	case 'submitHideBox':
		if(!loggedIn(true)) break;
		$boxType = t3lib_div::_GET('type');
		if($boxType) {		
			$res = $TYPO3_DB->exec_SELECTgetRows('*','tx_enotelms_userdata','crfeuser_id='.intval($user_id));
			if(!$res) {
				$field_values = array(
					'uid' => null,
					'hidden_boxenvs' => serialize(array(mysql_real_escape_string($boxType))),
					'crfeuser_id' => intval($user_id)
				);
				$TYPO3_DB->exec_INSERTquery('tx_enotelms_userdata',$field_values);
			} else {
				$hiddenBoxEnvs = unserialize($res[0]['hidden_boxenvs']);
				if(!is_array($hiddenBoxEnvs)){
					$hiddenBoxEnvs = array();
				}
				if(!in_array($boxType, $hiddenBoxEnvs)) {
					array_push($hiddenBoxEnvs,mysql_real_escape_string($boxType));
					$field_values = array(
						'hidden_boxenvs'=> serialize($hiddenBoxEnvs)
					);
					$TYPO3_DB->exec_UPDATEquery('tx_enotelms_userdata','crfeuser_id='.intval($user_id),$field_values);
				}
			}
		}
	break;
	case 'submitShowBox':
		if(!loggedIn(true)) break;
		$boxType = t3lib_div::_GET('type');
		if($boxType) {		
			$res = $TYPO3_DB->exec_SELECTgetRows('*','tx_enotelms_userdata','crfeuser_id='.intval($user_id));
			if(!$res) {
				break;
			} else {
				$hiddenBoxEnvs = unserialize($res[0]['hidden_boxenvs']);
				if(!is_array($hiddenBoxEnvs)){
					break;
				}
				if(!in_array($boxType, $hiddenBoxEnvs)) {
					break;
				} else {
					unset($hiddenBoxEnvs[array_search($boxType,$hiddenBoxEnvs)]);
					$field_values = array(
						'hidden_boxenvs'=> serialize($hiddenBoxEnvs)
					);
					$TYPO3_DB->exec_UPDATEquery('tx_enotelms_userdata','crfeuser_id='.intval($user_id),$field_values);
				}
			}
		}
	break;
	case 'getBoxMenu':
		header('Content-type: text/html');
		if($boxtype = t3lib_div::_GET('boxtype')) {
			$res = $TYPO3_DB->exec_SELECTgetRows('*','tx_enotelms_userdata','crfeuser_id='.intval($user_id));
			if(!($boxname = t3lib_div::_GET('boxtype'))) {
				$boxname = $boxtype;
			}
			$content = '';
			/*$content .= '
			<div class="listElement header">Denne eNote</div>
			<div class="listElement"><a href="#" onClick="hideBox(\''.$boxtype.'\',\''.$boxname.'\',false,\'hide\'); return false">Skjul midlertidigt</a></div>';*/
			if(loggedIn()) {
				$res = $TYPO3_DB->exec_SELECTgetRows('*','tx_enotelms_userdata','crfeuser_id='.intval($user_id));
				$boxHidden = false;
				if($res) {
					$hiddenBoxEnvs = unserialize($res[0]['hidden_boxenvs']);
					if(in_array($boxtype, $hiddenBoxEnvs)) {
						$boxHidden = true;
					}
				}
				$content .= '
					<div class="listElement header">Alle eNoter</div>
					<div class="listElement"><a href="#" onClick="hideBox(\''.$boxtype.'\',\''.$boxname.'\',true,\''.($boxHidden ? 'show' : 'hide').'\'); return false">'.($boxHidden?'Vis':'Sammenfold').' altid denne type indhold</a></div>';
			} else {
				$content = '<div style="padding:5px; background-color: #fff">Log ind for at se flere valgmuligheder</div>';
			}
		} else {
			$content .= 'No box type supplied';
		}
		echo $content;
	break;
	default:
		echo "No valid function passed";
}

?>